//! \file Behavior_TODO.cpp
//! \brief Implements the <code>fullsail_ai::fundamentals::Behavior</code> abstract class.
//! \author Jeremiah Blanchard with code from Cromwell D. Enage

#include <queue>
#include <stack>
#include "Behavior.h"

namespace fullsail_ai { namespace fundamentals {

	unsigned Behavior::createdCount = 0;
	unsigned Behavior::destroyedCount = 0;

	unsigned Behavior::getCreatedCount()
	{
		return createdCount;
	}

	unsigned Behavior::getDestroyedCount()
	{
		return destroyedCount;
	}

	Behavior::Behavior(char const* _description) : parent(0)
	{
		description = _description;
	}

	char const* Behavior::toString() const
	{
		return description;
	}

	bool Behavior::isLeaf() const
	{
		return false;
	}

	//! \todo
	//!   - Implement the <code>Behavior</code> methods.
	size_t Behavior::getChildCount() const
	{
		return children.size();
	}

	Behavior* Behavior::getChild(size_t index)
	{
		return children.at(index);
	}

	Behavior const* Behavior::getChild(size_t i) const
	{
		return children.at(i);
	}

	void Behavior::addChild(Behavior* child)
	{
		child->parent = this;
		children.push_back(child);
	}

	void Behavior::breadthFirstTraverse(void (*dataFunction)(Behavior const*)) const
	{
		std::queue<Behavior const*> behaviorQueue;
		behaviorQueue.push(this);

		while(behaviorQueue.size())
		{
			dataFunction(behaviorQueue.front());

			for(unsigned int i = 0; i < behaviorQueue.front()->children.size(); ++i)
			{
				behaviorQueue.push(behaviorQueue.front()->children.at(i));
			}

			behaviorQueue.pop();
		}
	}

	void Behavior::preOrderTraverse(void (*dataFunction)(Behavior const*)) const
	{
		dataFunction(this);
		
		for(unsigned int i = 0; i < children.size(); ++i)
		{
			children.at(i)->preOrderTraverse(dataFunction);
		}
	}

	void Behavior::postOrderTraverse(void (*dataFunction)(Behavior const*)) const
	{
		for(unsigned int i = 0; i < children.size(); ++i)
		{
			children.at(i)->postOrderTraverse(dataFunction);
		}

		dataFunction(this);
	}

}}  // namespace fullsail_ai::fundamentals
